<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form action="/register" method="post">
    用户名: <input type="text" name="tname" onblur="check()" id="username"> <span id="span"></span><br>
    密码: <input type="password" name="password"> <br>
    <input type="submit" value="注册">
</form>
<script>
    function check() {
        var username=$('#username').val();
        $.ajax({
            url:"/check",
            data:{tname:username},
            type:"post",
            success:function (data){
                if (data.code==200){
                    $("#span").html("用户名可用");
                    $("#span").css("color","green");

                }else if (data.code==400){
                    $("#span").html("用户名已存在");
                    $("#span").css("color","red");
                }

            },
            error:function (data){

                alert(data.msg);
            }
        })
    }
</script>
</body>
</html>